using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

namespace IQIGame.Onigao.Game.Editors
{
    public class EdtCfgFSMConditionPart
    {
        public string funcName;
        public List<Type> paramTypes = new List<Type>();
        public List<object> paramValues = new List<object>();
        public CompareOperation compareOP;
        public Type targetType;
        public object targetValue;
        public LogicOperation prevLogicOP;

        public bool isPublic
        {
            get
            {
                if (string.IsNullOrEmpty(funcName))
                {
                    return false;
                }
                return char.IsLower(funcName[0]);
            }
        }

        public bool Init(string condPartText, LogicOperation logicOP)
        {
            condPartText = condPartText.TrimStart();
            int parenthesisLIdx = condPartText.IndexOf("(");
            int parenthesisRIdx = condPartText.IndexOf(")");
            if (parenthesisLIdx == -1)
            {
                Debug.LogError("条件参数非法：" + condPartText);
                return false;
            }
            funcName = condPartText.Substring(0, parenthesisLIdx);
            if (parenthesisRIdx - parenthesisLIdx > 1)
            {
                string paramStr = condPartText.Substring(parenthesisLIdx + 1, parenthesisRIdx - parenthesisLIdx - 1);
                EdtCfgFSMUtil.ParseParamValues(paramStr, paramTypes, paramValues);
            }

            string strAfterFunction = condPartText.Substring(parenthesisRIdx + 1);
            string pattern = @"(==|!=|>=|>|<=|<)\s*(.*)";
            Match match = Regex.Match(strAfterFunction, pattern);
            string compareOPStr;
            string strBehindCompareOP;
            if (match.Success)
            {
                compareOPStr = match.Groups[1].Value;
                strBehindCompareOP = match.Groups[2].Value;
            }
            else
            {
                Debug.LogError("条件参数非法：" + condPartText);
                return false;
            }

            compareOP = ConvertCompareOperation(compareOPStr);
            EdtCfgFSMUtil.ParseParamValue(strBehindCompareOP, out targetType, out targetValue);

            prevLogicOP = logicOP;

            return true;
        }

        private CompareOperation ConvertCompareOperation(string compareOPStr)
        {
            switch (compareOPStr)
            {
                case "==":
                    return CompareOperation.Equal;
                case "!=":
                    return CompareOperation.NotEqual;
                case ">":
                    return CompareOperation.GreaterThan;
                case ">=":
                    return CompareOperation.GreaterEqual;
                case "<":
                    return CompareOperation.LessThan;
                case "<=":
                    return CompareOperation.LessEqual;
                default:
                    break;
            }
            UnityEngine.Debug.LogError("不支持的比较运算符：" + compareOPStr);
            return CompareOperation.None;
        }

        public CFSMConditionPartCfg ToConfig()
        {
            List<CFSMParamType> typeList = new List<CFSMParamType>();
            for (int i = 0; i < paramTypes.Count; i++)
            {
                Type type = paramTypes[i];
                typeList.Add((CFSMParamType)EdtCfgFSMUtil.ConvertType(type));
            }
            CFSMConditionPartCfg condPart = new CFSMConditionPartCfg();
            condPart.funcName = funcName;
            condPart.isPublic = isPublic;
            condPart.paramTypes = typeList;
            condPart.paramValues = new List<object>(paramValues);
            condPart.compareOPeration = compareOP;
            condPart.targetType = (CFSMParamType)EdtCfgFSMUtil.ConvertType(targetType);
            condPart.targetValue = targetValue;
            condPart.prevLogicOperation = prevLogicOP;
            return condPart;
        }

        public string GetCodeFileName(string fsmShortPath, out string codeModuleName)
        {
            codeModuleName = fsmShortPath.Replace("/", "_");
            string codeName = $"{EdtCFSMConst.CondFuncCSFilePrefix}_{codeModuleName}_{funcName}";
            return codeName;
        }
    }
}